package com.jess.arms.utils;

import android.app.Activity;

import androidx.fragment.app.Fragment;

import com.jess.arms.mvp.IView;
import com.jess.arms.utils.RxLifecycleUtils;
import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.android.FragmentEvent;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.Single;
import io.reactivex.SingleSource;
import io.reactivex.SingleTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * ================================================
 * 放置便于使用 RxJava 的一些工具方法
 * <p>
 * Created by MVPArmsTemplate on 01/07/2019 17:36
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * ================================================
 */
public class RxUtils {

    private RxUtils() {
    }

    public static <T> ObservableTransformer<T, T> bindRxlifecycle(final IView view) {
        LifecycleTransformer<T> t;
        if (view instanceof Activity) {
            t = RxLifecycleUtils.bindUntilEvent(view, ActivityEvent.DESTROY);
        } else {
            t = RxLifecycleUtils.bindUntilEvent(view, FragmentEvent.DESTROY_VIEW);
        }
        return new ObservableTransformer<T, T>() {
            @Override
            public Observable<T> apply(Observable<T> observable) {
                return observable.subscribeOn(AndroidSchedulers.mainThread()).observeOn(AndroidSchedulers.mainThread()).compose(t);
            }
        };
    }

    public static <T> ObservableTransformer<T, T> applySchedulers(final IView view) {
        LifecycleTransformer<T> t;
        if (view instanceof Activity) {
            t = RxLifecycleUtils.bindUntilEvent(view, ActivityEvent.DESTROY);
        } else {
            t = RxLifecycleUtils.bindUntilEvent(view, FragmentEvent.DESTROY_VIEW);
        }
        return new ObservableTransformer<T, T>() {
            @Override
            public Observable<T> apply(Observable<T> observable) {
                return observable.subscribeOn(Schedulers.io()).doOnSubscribe(disposable -> {
                    view.showLoading();//显示进度条
                }).subscribeOn(AndroidSchedulers.mainThread()).observeOn(AndroidSchedulers.mainThread()).doFinally(new Action() {
                    @Override
                    public void run() {
                        view.hideLoading();//隐藏进度条
                    }
                }).compose(t);
            }
        };
    }

    /**
     * single类型线程转换
     * @param view
     * @param <T>
     * @return
     */
    public static <T> SingleTransformer<T,T> applySingleSchedulers(final IView view){
        LifecycleTransformer<T> t;
        if (view instanceof Activity) {
            t = RxLifecycleUtils.bindUntilEvent(view, ActivityEvent.DESTROY);
        } else {
            t = RxLifecycleUtils.bindUntilEvent(view, FragmentEvent.DESTROY_VIEW);
        }
        return new SingleTransformer<T, T>() {
            @Override
            public SingleSource<T> apply(Single<T> upstream) {
                return upstream.subscribeOn(Schedulers.io()).doOnSubscribe(disposable -> {
                    view.showLoading();//显示进度条
                }).subscribeOn(AndroidSchedulers.mainThread()).observeOn(AndroidSchedulers.mainThread()).doFinally(new Action() {
                    @Override
                    public void run() {
                        view.hideLoading();//隐藏进度条
                    }
                }).compose(t);
            }
        };
    }


    public static <T> ObservableTransformer<T, T> applySchedulers(final IView view,String msg) {
        LifecycleTransformer<T> t;
        if (view instanceof Activity) {
            t = RxLifecycleUtils.bindUntilEvent(view, ActivityEvent.DESTROY);
        } else {
            t = RxLifecycleUtils.bindUntilEvent(view, FragmentEvent.DESTROY_VIEW);
        }
        return new ObservableTransformer<T, T>() {
            @Override
            public Observable<T> apply(Observable<T> observable) {
                return observable.subscribeOn(Schedulers.io()).doOnSubscribe(disposable -> {
                    view.showLoading(msg);//显示进度条
                }).subscribeOn(AndroidSchedulers.mainThread()).observeOn(AndroidSchedulers.mainThread()).doFinally(new Action() {
                    @Override
                    public void run() {
                        view.hideLoading();//隐藏进度条
                    }
                }).compose(t);
            }
        };
    }

    public static <T> ObservableTransformer<T, T> applyItemViewSchedulers(final IView view) {
        return new ObservableTransformer<T, T>() {
            @Override
            public ObservableSource<T> apply(Observable<T> observable) {
                return observable.subscribeOn(Schedulers.io()).doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        view.showLoading();//显示进度条
                    }
                }).subscribeOn(AndroidSchedulers.mainThread()).observeOn(AndroidSchedulers.mainThread()).doFinally(new Action() {
                    @Override
                    public void run() throws Exception {
                        view.hideLoading();//隐藏进度条
                    }
                });
            }
        };
    }

    public static <T> ObservableTransformer<T, T> applySchedulersNoLoading(final IView view) {
        LifecycleTransformer<T> t = null;
        if (view instanceof Activity) {
            t = RxLifecycleUtils.bindUntilEvent(view, ActivityEvent.DESTROY);
        } else if (view instanceof Fragment) {
            t = RxLifecycleUtils.bindUntilEvent(view, FragmentEvent.DESTROY_VIEW);
        }
        LifecycleTransformer<T> finalT = t;
        return new ObservableTransformer<T, T>() {
            @Override
            public Observable<T> apply(Observable<T> observable) {
                return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).compose(finalT);
            }
        };
    }

    public static <T> ObservableTransformer<T, T> applySchedulersNoBindingRxLife(final IView view) {
        //        LifecycleTransformer<T> t = null;
        //        if (view instanceof Activity) {
        //            t = RxLifecycleUtils.bindUntilEvent(view, ActivityEvent.DESTROY);
        //        } else if (view instanceof Fragment) {
        //            t = RxLifecycleUtils.bindUntilEvent(view, FragmentEvent.DESTROY);
        //        }
        //        LifecycleTransformer<T> finalT = t;
        return new ObservableTransformer<T, T>() {
            @Override
            public Observable<T> apply(Observable<T> observable) {
                return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
            }
        };
    }
}
